package org.monte.previo2;

import java.util.List;

import org.monte.previo2.base.ManagerDB;
import org.monte.previo2.clases.encuesta.Applicacion;
import org.monte.previo2.clases.encuesta.Seccion;

import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class SeccionListFragment extends ListFragment {

    private static final String STATE_ACTIVATED_POSITION = "activated_position";
    ManagerDB manager;
    Applicacion app;

    private Callbacks mCallbacks = sDummyCallbacks;
    private int mActivatedPosition = ListView.INVALID_POSITION;

    public interface Callbacks {
        public void onItemSelected(String id,String id2);
    }

    private static Callbacks sDummyCallbacks = new Callbacks() {        
        public void onItemSelected(String id,String id2) {
        }
    };

    public SeccionListFragment() {}

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        int layout = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) ?
            android.R.layout.simple_list_item_activated_1 :
            android.R.layout.simple_list_item_checked;
        
        manager= new ManagerDB(getActivity());
		
		app = manager.getApplicacionById(getActivity().getIntent().getStringExtra("idApp"));
				
		final List<Seccion> secciones =manager.getSeccionesBYEncuesta(app.getIdEncuesta());		

        setListAdapter(new ArrayAdapter<Seccion>(getActivity(),layout, secciones));	
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);               
        if (savedInstanceState != null && savedInstanceState
                .containsKey(STATE_ACTIVATED_POSITION)) {
            setActivatedPosition(savedInstanceState.getInt(STATE_ACTIVATED_POSITION));
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (!(activity instanceof Callbacks)) {
            throw new IllegalStateException("Activity must implement fragment's callbacks.");
        }

        mCallbacks = (Callbacks) activity;
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mCallbacks = sDummyCallbacks;
    }

    @Override
    public void onListItemClick(ListView listView, View view, int position, long id) {
        super.onListItemClick(listView, view, position, id);
        Seccion secc=(Seccion)listView.getAdapter().getItem(position);
        mCallbacks.onItemSelected(app.getIdAplicacion(),secc.getIdSeccion());
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        if (mActivatedPosition != ListView.INVALID_POSITION) {
            outState.putInt(STATE_ACTIVATED_POSITION, mActivatedPosition);
        }
    }

    public void setActivateOnItemClick(boolean activateOnItemClick) {
        getListView().setChoiceMode(activateOnItemClick
                ? ListView.CHOICE_MODE_SINGLE
                : ListView.CHOICE_MODE_NONE);
    }

    public void setActivatedPosition(int position) {
        if (position == ListView.INVALID_POSITION) {
            getListView().setItemChecked(mActivatedPosition, false);
        } else {
            getListView().setItemChecked(position, true);
        }

        mActivatedPosition = position;
    }
}
